package com.pay.cloud.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.pay.cloud.BindState;
import com.pay.cloud.IconFontFactory;
import com.pay.cloud.R;
import com.pay.cloud.SubScreenFactory;


public class RefundScanFragment extends AbstractScanFragment implements BindState {

    @Override
    public boolean useNetwork() {
        return true;
    }

    @Override
    public void onPause() {
        super.onPause();
        SubScreenFactory.startSubScreenToWelcome(getContext());
    }

    @Override
    protected void execute() throws Exception {
        speak("请扫码退款");
        TipVerticalFragment fragment = new TipVerticalFragment(TipFragment.TipType.WAIT, "请扫描订单码进行退款");
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(getView().getId(), fragment);
        fragmentTransaction.commit();
        SubScreenFactory.startSubScreen(this::flushSubScreenToRefundScan);
    }

    private void flushSubScreenToRefundScan() {
        Context context = getContext();
        if (null == context) {
            return;
        }

        Bitmap bitmap = SubScreenFactory.createBitmap(context);
        Canvas canvas = new Canvas(bitmap);
        //canvas.drawColor(Color.WHITE);

        Paint iconPaint = new Paint();
        iconPaint.setAntiAlias(true);
        iconPaint.setTextSize(70f);
        iconPaint.setColor(context.getColor(R.color.colorWarning));
        Typeface typeface = IconFontFactory.getHalflings(context);
        iconPaint.setTypeface(typeface);

        String icon = getString(R.string.glyphicon_exclamation_sign);
        int iconWidth = (int) iconPaint.measureText(icon);
        int startX = (bitmap.getWidth() - iconWidth) / 2;
        canvas.drawText(icon, startX, 150, iconPaint);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(30f);

        String text = "请扫码退款";
        int fontWidth = (int) paint.measureText(text);
        startX = (bitmap.getWidth() - fontWidth) / 2;
        canvas.drawText(text, startX, 220, paint);

        canvas.save();
        canvas.restore();

        SubScreenFactory.flush(bitmap);

    }

}
